﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Metamorph.Core.Bpm.Connectors;

namespace Metamorph.Core.Bpm.Activities
{
	public class LoopTask : Task, ILoopActivity
	{
		protected int		iMaximumCondition;
		protected string	sExpressionCondition;

		public LoopTask()
			: base()
		{
		}

		public LoopTask(string sName)
			: base(sName)
		{
		}

		public LoopTask(string sName, string sActionName)
			: base(sName, sActionName)
		{
		}

		#region IShape Members

		public override void Init(IBusinessProcess bpBusinessProcess)
		{
			if (bpBusinessProcess.ExecutionStack.Peek().OwnerShape != this)
			{
				bpBusinessProcess.ExecutionStack.Push(new ExecutionContext(this, bpBusinessProcess, this, 0));

				MetamorphManager.Instance.EventManager.Subscribe(lEvents, bpBusinessProcess);
			}
		}

		public override void Next(IBusinessProcess bpBusinessProcess)
		{
			ExecutionContext ecExecutionContext = bpBusinessProcess.ExecutionStack.Peek();

			if (ecExecutionContext.CurrentIteration < iMaximumCondition)
			{
				ActionDispatcher.Instance.Execute(sActionName, bpBusinessProcess.ProcessContext);

				ecExecutionContext.CurrentIteration++;
			}
			else
			{
				bpBusinessProcess.ExecutionStack.Pop();

				bpBusinessProcess.ExecutionStack.Peek().CurrentShape = tTransition.Target;
			}
		}

		#endregion

		#region ILoopActivity Members

		public int MaximumCondition
		{
			get
			{
				return iMaximumCondition;
			}
			set
			{
				iMaximumCondition = value;
			}
		}

		public string ExpressionCondition
		{
			get
			{
				return sExpressionCondition;
			}
			set
			{
				sExpressionCondition = value;
			}
		}

		#endregion
	}
}
